package com.gz.thread.runnable;

/**
 * 模拟龟兔赛跑
 */
public class Race implements Runnable {

    private static String winner;

    public void run() {
        for (int i = 0; i <= 100; i++) {
            if (isOver(i)) {
                break;  //存在获胜者则种植比赛
            }
            System.out.println(Thread.currentThread().getName() + "跑了：" + i + "步");
        }
    }

    public boolean isOver(int s) {

        if (winner != null) {
            return true;  //如果有获胜者则终止比赛
        }
        if (s >= 100) {
            winner = Thread.currentThread().getName(); // 获取获胜者
            System.out.println(Thread.currentThread().getName() + "获胜。。。。");
            return true;
        } else
            return false;
    }

    public static void main(String[] args) {
        Race race = new Race();
        new Thread(race, "兔子").start();
        new Thread(race, "乌龟").start();
    }
}
